package com.atguigu.edu.controller;


import com.atguigu.commonUtils.R;
import com.atguigu.edu.entity.Teacher;
import com.atguigu.edu.entity.vo.TeacherQuery;
import com.atguigu.edu.service.TeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Rayyy666
 * @since 2022-04-01
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/edu/teacher")
//@CrossOrigin //解决跨域
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    /**
     * 查询所有讲师的数据
     *
     * @return
     */
    @ApiOperation("所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher() {
        List<Teacher> list = teacherService.list(null);

        return R.ok().data("items", list).message("获取讲师列表成功");
    }

    /**
     * 按照id删除讲师
     *
     * @param id
     * @return
     * @PathVariable : 获取到路径变量中的值
     */
    @ApiOperation(value = "根据id逻辑删除讲师", notes = "根据id逻辑删除讲师")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id) {
        boolean result = teacherService.removeById(id);

        if (result) {
            return R.ok().message("讲师删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    /**
     * 分页查询讲师列表
     *
     * @param page  当前页码
     * @param limit 每页记录数
     * @return
     */
    @ApiOperation("分页讲师列表")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit) {

        Page<Teacher> pageParam = new Page<>(page, limit);
        IPage<Teacher> pageModel = teacherService.page(pageParam);
        List<Teacher> records = pageModel.getRecords();
        long total = pageModel.getTotal();

//        teacherService.page(pageParam,null);
//        long total = pageParam.getTotal();
//        List<Teacher> records = pageParam.getRecords();

//        HashMap map = new HashMap<>();
//        map.put("total",total);
//        map.put("records",records);
//        return R.ok().data(map);

        try{
            int i = 10/0;
        }catch(Exception e){
            //执行了自定义异常
            throw new GuliException(20001,"执行了自定义的异常处理.......GuliException");
        }

        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 带分页的 条件查询    讲师列表
     *
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     * @RequestBody : 使用json格式传递数据，把 json数据封装到对应的对象里面去 需要使用post提交方式
     * @ResponseBody : 返回json数据
     */
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(value = "当前页码", required = true) @PathVariable long current,
                                  @ApiParam(value = "每页记录数", required = true) @PathVariable long limit,
                                  @ApiParam(value = "讲师列表查询对象", required = true)
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {

        //创建page对象,将 当前页码 和 每页记录数 传进去
        Page<Teacher> pageTeacher = new Page<>(current,limit);

        //构造条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();

        //多条件组合查询 mybatis 动态sql
        //判断条件值是否为空 如果不为空拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            //ge 大于等于
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            // le 小于等于
            wrapper.le("gmt_modified", end);
        }
        //排序 desc降序排序
        wrapper.orderByDesc("gmt_create");

        //调用service实现条件查询分页
        teacherService.page(pageTeacher, wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<Teacher> records = pageTeacher.getRecords();//数据list集合

//        System.out.println(records);
        return R.ok().data("total", total).data("records", records);

    }

    /**
     * 添加讲师
     *
     * @param teacher
     * @return
     */
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);

        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    /**
     * 根据 讲师id 查询讲师
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取讲师信息")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@ApiParam(value = "讲师ID",required = true)@PathVariable String id) {

        Teacher teacher = teacherService.getById(id);
        if(teacher != null){
            return R.ok().data("teacher", teacher);
        }else{
            return R.error().message("数据不存在");
        }

    }

    /**
     * 根据讲师id修改讲师
     *
     * @param teacher
     * @return
     */
    @ApiOperation("更新讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@ApiParam(value = "讲师对象",required = true)@RequestBody Teacher teacher) {

        boolean b = teacherService.updateById(teacher);
        if (b) {
            return R.ok().message("讲师修改成功");
        } else {
            return R.error().message("数据不存在");
        }
    }


}

